﻿using Microsoft.Xrm.Client;
using Microsoft.Xrm.Client.Metadata;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Client;
using System;
using System.Data.Services.Common;
using System.Runtime.Serialization;

namespace Sustainalytics.DynamicsCRMGateway.Entities
{
    [DataContractAttribute(IsReference = true)]
    [EntityLogicalNameAttribute("new_accountfeature")]
    [DataServiceKeyAttribute("Id")]
    [EntityAttribute("new_accountfeatureSet")]
    public class AccountFeature : CrmEntity
    {
        public AccountFeature() :
            base(EntityLogicalName)
        {
        }

        public const string EntityLogicalName = "new_accountfeature";

        [AttributeLogicalNameAttribute("new_accountfeatureid")]
        public Nullable<Guid> AccountFeatureId
        {
            get { return this.GetAttributeValue<Nullable<Guid>>("new_accountfeatureid"); }
            set { this.SetPrimaryIdAttributeValue<Nullable<Guid>>("new_accountfeatureId", "new_accountfeatureid", value); }
        }

        [AttributeLogicalNameAttribute("new_accountfeatureid")]
        public override Guid Id
        {
            get { return base.Id; }
            set { this.AccountFeatureId = value; }
        }

        [AttributeLogicalNameAttribute("new_name")]
        public string Name
        {
            get { return this.GetAttributeValue<string>("new_name"); }
            set { this.SetAttributeValue<string>("new_name", "new_name", value); }
        }

        [AttributeLogicalNameAttribute("new_account")]
        public CrmEntityReference AccountRef
        {
            get { return this.GetAttributeValue<EntityReference>("new_account"); }
            set { this.SetAttributeValue<EntityReference>("new_Account", "new_account", value); }
        }

        [AttributeLogicalNameAttribute("new_account")]
        [RelationshipSchemaNameAttribute("new_account_new_accountfeature_Account")]
        public Account Account
        {
            get { return this.GetRelatedEntity<Account>("new_account_new_accountfeature_Account", null); }
            set { this.SetRelatedEntity<Account>("new_account_new_accountfeature_Account", "new_account_new_accountfeature_Account", null, value); }
        }

        [AttributeLogicalNameAttribute("new_feature")]
        public CrmEntityReference FeatureRef
        {
            get { return this.GetAttributeValue<EntityReference>("new_feature"); }
            set { this.SetAttributeValue<EntityReference>("new_Feature", "new_feature", value); }
        }

        [AttributeLogicalNameAttribute("new_feature")]
        [RelationshipSchemaNameAttribute("new_new_feature_new_accountfeature_Feature")]
        public Feature Feature
        {
            get { return this.GetRelatedEntity<Feature>("new_new_feature_new_accountfeature_Feature", null); }
            set { this.SetRelatedEntity<Feature>("new_new_feature_new_accountfeature_Feature", "new_new_feature_new_accountfeature_Feature", null, value); }
        }

        /// <summary>
        /// Constructor for populating via LINQ queries given a LINQ anonymous type
        /// <param name="anonymousType">LINQ anonymous type.</param>
        /// </summary>
        public AccountFeature(object anonymousType) :
            this()
        {
            foreach (var p in anonymousType.GetType().GetProperties())
            {
                var value = p.GetValue(anonymousType, null);
                if (p.PropertyType == typeof(Guid))
                {
                    // Type is Guid, must be Id
                    base.Id = (Guid)value;
                    Attributes["new_accountfeatureid"] = base.Id;
                }
                else if (p.Name == "FormattedValues")
                {
                    // Add Support for FormattedValues
                    FormattedValues.AddRange((FormattedValueCollection)value);
                }
                else
                {
                    Attributes[p.Name.ToLowerInvariant()] = value;
                }
            }
        }
    }
}
